<?php namespace web\admin\controller;
	
	class Goods extends Common{
		private $db;
		public function __construct(){
			parent::__construct();
			$this->db=new \system\model\Goods;
		}
		
	//首页
	public function index() {
		View::with('title','商品添加');
		
		
		
		
//		获取数据返回页面
//		$data=join('type','goods.gid','=','class.cid')->get();
	$data=Db::table('goods')
    ->join('category','goods.cid','=','category.cid')
    ->where('is_recycle',0)->get();
    foreach ($data as $k => $v) {
    	$data[$k]['kucun']=array_sum(Db::table('goods_list')->where('gid',$v['gid'])->lists('inventory'));
    }
//  p($data);
//  die;
//  p($data);die;
		View::with('data',$data);
		View::make();
	}
//		商品添加
		public function add(){
			if(IS_POST){
				$re=$this->db->store(0);
				if(!$re){
					message($this->db->getError());
				}
				message('添加成功','index');
			}
			
//			获取类型数据
			$brandData=Db::table('brand')->get();
			View::with('brandData',$brandData);
//			获取分类数据
			$cateData=Db::table('category')->get();
			$cateData=Data::tree($cateData,'cname','cid','pid');
			View::with('cateData',$cateData);
			$tid=Q('post.tid',0,'intval');
			View::make();
		}
		
//		获取商品类型数据返回到页面
		public function typeData(){
			$tid=Q('post.tid',0,'intval');
//			p($tid);die;
			$attrData=Db::table('type_attr')->where('tid',$tid)->get();
//			var_dump($attrData);die;
			foreach($attrData as $k=>$v){
				$attrData[$k]['tavalue']=explode('|',$v['tavalue']);
			}
			message($attrData,'','success');
		}
		
//		放入回收站
		public function cyre(){
			$gid=Q('get.gid',0,'intval');
			$re=$this->db->where('gid',$gid)->update(array('is_recycle'=>1));
			if(!$re){
				message('放入回收站失败',U('index'));
			}
			message('放入回收站成功',U('recycle'));
		}
//		回收站列表
		public function cyreList(){
//			获取数据
			$reData=$this->db->where('is_recycle',1)->get();
			View::with('reData',$reData);
			View::make();
		}
//		还原商品
		public function reduc(){
			$gid=Q('get.gid',0,'intval');
			$re=$this->db->where('gid',$gid)->update(array('is_recycle'=>0));
			if(!$re){
				message('还原失败',U('cyreList'));
			}
			message('还原成功',U('index'));
		}
//		删除商品
		public function del(){
			$attrModel=new \system\model\goods_attr;
			$detail=new \system\model\detail;
			$gid=Q('get.gid',0,'intval');
//			删除商品属性
			$attrModel->where('gid',$gid)->delete();
//			删除商品详情
			$detail->where('gid',$gid)->delete();
//			删除商品列表
			$this->db->where('gid',$gid)->delete();
			message('删除成功','index');
		}
		
//		编辑商品
		public function edit(){
//			获取get参数
			$gid=Q('get.gid',0,'intval');
//			p($gid);die;		
			if(IS_POST){
				$_POST['gid']=$gid;
//				p($_POST);die;
				$re=$this->db->store(1);
				if(!$re){
					message($this->db->getError());
				}
				message('编辑成功','index');

			}

//			p($gid);die;

//			获取分类数据
			$cateData=Db::table('category')->get();
			$cateData=Data::tree($cateData,'cname','cid','pid');
			View::with('cateData',$cateData);
//			获取品牌数据
			$brandData=Db::table('brand')->get();
			View::with('brandData',$brandData);
//			获取商品表数据
			$goodData=$this->db
			->join('detail','goods.gid','=','detail.gid')
			->where('goods.gid',$gid)
			->first();
//			p($goodData);die;
			$goodData['photo']=explode('|',$goodData['photo']);
//			p($goodData);die;
			View::with('goodData',$goodData);
		    
			
//			获取选中的类型属性数据
			$attrData=Db::table('goods')
			->join('category','goods.cid','=','category.cid')
			->join('type_attr','category.tid','=','type_attr.tid')
			->where('goods.gid',$gid)
			->where('type_attr.class',0)->get();
		    foreach($attrData as $k=>$v){
				$attrData[$k]['tavalue']=explode('|',$v['tavalue']);
			}
			View::with('attrData',$attrData);
//			获取所有类型属性
			$typeData1=Db::table('goods_attr')
			->join('type_attr','goods_attr.taid','=','type_attr.taid')
			->where('type_attr.class',0)
			->lists('gtvalue');
//			p($typeData1);die;
			View::with('typeData1',$typeData1);
			$typeData2=Db::table('type_attr')
			->Join('goods_attr','type_attr.taid','=','goods_attr.taid')
			->where('gid',$gid)
			->where('class',1)
			->orderBy('goods_attr.gtid','ASC')
			->get();
			foreach($typeData2 as $k=>$v){
				$typeData2[$k]['tavalue']=explode('|',$v['tavalue']);
			}
			View::with('typeData2',$typeData2);
			View::make();
		}
		
//		删除图片
		public function noDel(){
			$pic=Q('post.pic','');
			$gid=Q('post.gid','');
			if($pic=='resource/images/nopic.jpg'){
				return;
			}
			var_dump($pic);die;
			$photo=Db::table('detail')->where('gid',$gid)->first();
//			p($photo);
			$photo=explode('|',$photo['photo']);
			foreach ($photo as $k => $v) {
				if($pic!=$v){
					$photos[]=$v;
				}
			}
//			p($photos);die;
			$photos=implode('|', $photos);
			$photo=Db::table('detail')->where('gid',$gid)->update(array('photo'=>$photos));
			
//		判断文件是否存在
			if(file_exists($pic)){
//		删除文件
				unlink($pic);
			}
			
		}
		
		
		
	}
	
	
	
	
?>